package org.jeecg.modules.flow.utils;

import org.activiti.bpmn.model.ExtensionElement;
import org.activiti.bpmn.model.FlowElement;
import org.activiti.bpmn.model.UserTask;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 流程工具类
 */
public class FlowUtils {

    /**
     * 获取节点的扩展属性
     *
     * @param flowElement
     * @param name
     * @return
     */
    public static String getExtAttributes(FlowElement flowElement, String name) {
        if (flowElement instanceof UserTask) {
            List<ExtensionElement> extensionElements = flowElement.getExtensionElements().get("properties");
            List<Map<String, List<ExtensionElement>>> childElements = new ArrayList<>();

            if (extensionElements.isEmpty()) {
                return null;
            }
            extensionElements.forEach(extensionElement -> {
                childElements.add(extensionElement.getChildElements());
            });
            for (Map<String, List<ExtensionElement>> map : childElements) {
                for (ExtensionElement extEl : map.get("property")) {
                    if (extEl.getAttributes().get("name").get(0).getValue().equals(name)) {
                        return extEl.getAttributes().get("value").get(0).getValue();
                    }
                }
            }
        }
        return null;
    }

}
